package com.goodfox.web.biz.forest.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.goodfox.web.biz.forest.entity.CountyStatisticsResult;
import com.goodfox.web.biz.forest.entity.ForestStatisticsResult;
import com.goodfox.web.biz.forest.entity.TownshipStatisticsResult;
import com.goodfox.web.biz.forest.entity.VillageStatisticsResult;
import com.goodfox.web.biz.forest.mapper.StatisticsMapper;
import com.goodfox.web.biz.forest.service.StatisticsService;
import com.goodfox.web.generic.area.entity.Area;
import com.goodfox.web.generic.area.mapper.AreaMapper;
import com.goodfox.web.generic.area.service.AreaService;
import com.goodfox.web.generic.provide.annotation.NoteExplain;

/**
 * 统计Service实现类，用于林权林地的各种统计
 * 
 * @author CXO Sun
 * 
 */
@Transactional
@Service(value = "statisticsService")
@NoteExplain(comment = "林权证统计")
public class StatisticsServiceImpl implements StatisticsService {
	
	/** log4j日志器 */
	private Logger logger=Logger.getLogger(StatisticsServiceImpl.class);
	
	/** 林权证统计Mapper */
	@Autowired
	private StatisticsMapper statisticsMapper;

	/** 坐落区域Mapper */
	@Autowired
	private AreaMapper areaMapper;
	
	@Autowired
	private AreaService areaService;

	/**
	 * 根据条件统计林权证,林地信息
	 * 
	 * @param queryMap
	 *            查询条件HashMap
	 * @return 统计结果实体
	 */
	@Override
	@NoteExplain(comment = "根据条件统计林权证,林地信息")
	public List<ForestStatisticsResult> forestStatistics(Map queryMap) {
		List<ForestStatisticsResult> countyResultList = new ArrayList<ForestStatisticsResult>();
		String areaId=null;
		if(queryMap.get("areaId")!=null && !String.valueOf(queryMap.get("areaId")).equals("")){
			areaId=String.valueOf(queryMap.get("areaId"));
	    }
		Set<Area> areaSet=areaService.queryAreaSetByMultiId(areaId);
		for(Area county:areaSet){
			queryMap.put("areaId", county.getAreaId());
			queryMap.put("areaGrade", county.getAreaGrade());
			ForestStatisticsResult countyResult=statisticsMapper.selectForestStatisticsResult(queryMap);
			countyResult.setSubResult(new ArrayList<ForestStatisticsResult>());
			countyResultList.add(countyResult);
			if(county.getRealSubAreaSet()!=null){
				for(Area township:county.getRealSubAreaSet()){
					queryMap.put("areaId", township.getAreaId());
					queryMap.put("areaGrade", township.getAreaGrade());
					ForestStatisticsResult townshipResult=statisticsMapper.selectForestStatisticsResult(queryMap);
					townshipResult.setSubResult(new ArrayList<ForestStatisticsResult>());
					countyResult.getSubResult().add(townshipResult);
					if(township.getRealSubAreaSet()!=null){
						for(Area village:township.getRealSubAreaSet()){
							queryMap.put("areaId", village.getAreaId());
							queryMap.put("areaGrade", village.getAreaGrade());
							ForestStatisticsResult villageResult=statisticsMapper.selectForestStatisticsResult(queryMap);
							townshipResult.getSubResult().add(villageResult);
						}
					}
				}
			}
		}
		return countyResultList;
	}

	@NoteExplain(comment = "导出统计信息")
	public List<CountyStatisticsResult> exportForestStatisticsList(Map queryMap){
		List<CountyStatisticsResult> countyResultList = new ArrayList<CountyStatisticsResult>();
		String areaId=null;
		if(queryMap.get("areaId")!=null && !String.valueOf(queryMap.get("areaId")).equals("")){
			areaId=String.valueOf(queryMap.get("areaId"));
	    }
		Set<Area> areaSet=areaService.queryAreaSetByMultiId(areaId);
		for(Area county:areaSet){
			queryMap.put("areaId", county.getAreaId());
			queryMap.put("areaGrade", county.getAreaGrade());
			ForestStatisticsResult countyResult=statisticsMapper.selectForestStatisticsResult(queryMap);
			CountyStatisticsResult countyStatistics=new CountyStatisticsResult();
			countyStatistics.setCountyName(countyResult.getAreaName());
			countyStatistics.setCountyMsg(countyResult.getForestRightsNum()+"林权证/"+countyResult.getWoodlandNum()+"林地/"+countyResult.getWoodlandAcreage()+"亩");
			countyStatistics.setTownshipResultList(new ArrayList<TownshipStatisticsResult>());
			countyResultList.add(countyStatistics);
			if(county.getRealSubAreaSet()!=null){
				for(Area township:county.getRealSubAreaSet()){
					queryMap.put("areaId", township.getAreaId());
					queryMap.put("areaGrade", township.getAreaGrade());
					ForestStatisticsResult townshipResult=statisticsMapper.selectForestStatisticsResult(queryMap);
					TownshipStatisticsResult townshipStatistics=new TownshipStatisticsResult();
					townshipStatistics.setTownshipName(townshipResult.getAreaName());
					townshipStatistics.setTownshipMsg(townshipResult.getForestRightsNum()+"林权证/"+townshipResult.getWoodlandNum()+"林地/"+townshipResult.getWoodlandAcreage()+"亩");
					townshipStatistics.setVillageResultList(new ArrayList<VillageStatisticsResult>());
					countyStatistics.getTownshipResultList().add(townshipStatistics);
					if(township.getRealSubAreaSet()!=null){
						for(Area village:township.getRealSubAreaSet()){
							queryMap.put("areaId", village.getAreaId());
							queryMap.put("areaGrade", village.getAreaGrade());
							ForestStatisticsResult villageResult=statisticsMapper.selectForestStatisticsResult(queryMap);
							VillageStatisticsResult villageStatistics=new VillageStatisticsResult();
							villageStatistics.setVillageName(villageResult.getAreaName());
							villageStatistics.setVillageMsg(villageResult.getForestRightsNum()+"林权证/"+villageResult.getWoodlandNum()+"林地/"+villageResult.getWoodlandAcreage()+"亩");
							townshipStatistics.getVillageResultList().add(villageStatistics);
						}
					}
				}
			}
		}
		return countyResultList;
	}

}
